﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LIM.controller;
using Telerik.WinControls.UI;
using Telerik.WinControls.Data;

namespace LIM.forms.uc_CRM_HopDong_ChiTiet
{
    public partial class uc_CRM_HopDong_ChiTiet_DieuKhoan : UserControl
    {
        public uc_CRM_HopDong_ChiTiet_DieuKhoan()
        {
            InitializeComponent();

            VNThanhLoad();
        }

        //\\VNThanh
        void VNThanhLoad()
        {
            // Gan su kien.
            this.radGridView2.UserAddedRow += new GridViewRowEventHandler(radGridView2_UserAddedRow);
            this.radGridView2.UserDeletedRow += new GridViewRowEventHandler(radGridView2_UserDeletedRow);
            this.radGridView2.CellFormatting += new CellFormattingEventHandler(radGridView2_CellFormatting);
            this.radGridView2.CellValidating += new CellValidatingEventHandler(radGridView2_CellValidating);

            this.radMenuItem1.Click += new EventHandler(radMenuItem1_Click);
            this.radMenuItem3.Click += new EventHandler(radMenuItem3_Click);
            this.radMenuItem4.Click += new EventHandler(radMenuItem4_Click);

            LoadDataGrid();
        }

        public event ValueChangingEventHandler EvtUserAddedRow;
        private LIM.controller.Thanh_CRMHopDongChiTietController _Controller;
        public LIM.controller.Thanh_CRMHopDongChiTietController Controller
        {
            get { if (_Controller == null) _Controller = new Thanh_CRMHopDongChiTietController(); return _Controller; }
            set { _Controller = value; }
        }
        System.Windows.Forms.BindingSource _CbbKHBindingSource = new BindingSource();
        System.Windows.Forms.BindingSource _GridCTBindingSource = new BindingSource();
        public List<Thanh_DieuKhoanThanhToan> LstThanh_DieuKhoanThanhToan = new List<Thanh_DieuKhoanThanhToan>();
        object _OldThanhTien = null;

        // Load DataGrid
        void LoadDataGrid()
        {
            // Binding source.
            _GridCTBindingSource.DataSource = LstThanh_DieuKhoanThanhToan;
            this.radGridView2.DataSource = _GridCTBindingSource;
            this.radGridView2.CurrentRow = null;
        }

        public void RefreshControl()
        {
            SetControlValues(LstThanh_DieuKhoanThanhToan);
        }
        // Refresh.
        void radMenuItem1_Click(object sender, EventArgs e)
        {
            RefreshControl();
        }

        // Move up click.
        void radMenuItem3_Click(object sender, EventArgs e)
        {
            MoveUp();
        }

        // Move down click.
        void radMenuItem4_Click(object sender, EventArgs e)
        {
            MoveDown();
        }

        // Set STT sau khi chap nhan record.
        void radGridView2_UserAddedRow(object sender, GridViewRowEventArgs e)
        {
            e.Row.Cells["STT"].Value = this.radGridView2.Rows.Count;
        }

        // Delete multi row and reset STT.
        void radGridView2_UserDeletedRow(object sender, GridViewRowEventArgs e)
        {
            SetControlValues(LstThanh_DieuKhoanThanhToan);
        }

        // Chuyen format cho "DonGia" va "ThanhTien".
        void radGridView2_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            decimal value = 0;
            switch (e.CellElement.ColumnInfo.Name)
            {
                case "SoTien":
                    {
                        if (e.CellElement.Value != null && decimal.TryParse(e.CellElement.Value.ToString(), out value))
                        {
                            e.CellElement.Text = string.Format("{0:#,### (đ)}", value);
                        }
                        break;
                    }
                case "GiaTriHopDong":
                    {
                        if (e.CellElement.Value != null && decimal.TryParse(e.CellElement.Value.ToString(), out value))
                        {
                            e.CellElement.Text = string.Format("{0:#,###}", value);
                        }
                        break;
                    }
            }
        }

        void radGridView2_CellValidating(object sender, CellValidatingEventArgs e)
        {
            if (this.radGridView2.IsInEditMode)
            {
                switch (e.Column.Name)
                {
                    case "MoTa":
                        {
                            e.Cancel = e.Value == null || string.IsNullOrWhiteSpace(e.Value.ToString());
                            if (e.Cancel)
                                e.Row.ErrorText = "Mô tả không được rỗng.";
                            break;
                        }

                    case "SoTien":
                        {
                            decimal dSoTien = Convert.ToDecimal(e.Value);
                            if (dSoTien < 1)
                            {
                                e.Cancel = true;
                                e.Row.ErrorText = "Vui lòng nhập vào số tiền lớn hơn 0.";
                            }
                            break;
                        }
                    case "GiaTriHopDong":
                        {
                            int iGiaTriHopDong = 0;
                            if (e.Value == null || !int.TryParse(e.Value.ToString(), out iGiaTriHopDong) || iGiaTriHopDong < 1)
                            {
                                e.Cancel = true;
                                e.Row.ErrorText = "Vui lòng nhập vào giá trị hợp đồng lớn hớn 0.";
                            }
                            break;
                        }
                }
            }
        }

        // Dua du lieu tu doi tuong Thanh_DieuKhoanThanhToan vao Grid.
        public void SetControlValues(List<Thanh_DieuKhoanThanhToan> pLstThanh_DieuKhoanThanhToan)
        {
            LstThanh_DieuKhoanThanhToan = pLstThanh_DieuKhoanThanhToan;
            _GridCTBindingSource.DataSource = LstThanh_DieuKhoanThanhToan;
            this.radGridView2.DataSource = null;
            this.radGridView2.DataSource = _GridCTBindingSource;
            for (int i = 0; i < this.radGridView2.Rows.Count; i++)
            {
                this.radGridView2.CurrentRow = this.radGridView2.Rows[i];
                this.radGridView2.CurrentRow.Cells["STT"].Value = i + 1;
            }
        }

        // Move up
        public void MoveUp()
        {
            if (this.radGridView2.CurrentRow != null && this.radGridView2.CurrentRow.Index > 0 && this.radGridView2.CurrentRow.Index < this.radGridView2.Rows.Count)
            {
                int iIndex = this.radGridView2.CurrentRow.Index;
                Thanh_DieuKhoanThanhToan oThanh_DieuKhoanThanhToan = LstThanh_DieuKhoanThanhToan[iIndex];
                LstThanh_DieuKhoanThanhToan.RemoveAt(iIndex);
                LstThanh_DieuKhoanThanhToan.Insert(iIndex - 1, oThanh_DieuKhoanThanhToan);
                oThanh_DieuKhoanThanhToan.STT = iIndex;
                LstThanh_DieuKhoanThanhToan[iIndex].STT = iIndex + 1;
                RefreshControl();
                this.radGridView2.CurrentRow = this.radGridView2.Rows[iIndex - 1];
            }
        }

        // Move down
        public void MoveDown()
        {
            if (this.radGridView2.CurrentRow != null && this.radGridView2.CurrentRow.Index > -1 && this.radGridView2.CurrentRow.Index < this.radGridView2.Rows.Count - 1)
            {
                int iIndex = this.radGridView2.CurrentRow.Index;
                Thanh_DieuKhoanThanhToan oThanh_DieuKhoanThanhToan = LstThanh_DieuKhoanThanhToan[iIndex];
                LstThanh_DieuKhoanThanhToan.RemoveAt(iIndex);
                LstThanh_DieuKhoanThanhToan.Insert(iIndex + 1, oThanh_DieuKhoanThanhToan);
                oThanh_DieuKhoanThanhToan.STT = iIndex + 2;
                LstThanh_DieuKhoanThanhToan[iIndex].STT = iIndex + 1;
                RefreshControl();
                this.radGridView2.CurrentRow = this.radGridView2.Rows[iIndex + 1];
            }
        }
    }
}
